home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Celestin Apprentice 5
/
Apprentice-Release5.iso
/
Source Code
/
By the Book
/
Learn C++ (CodeWarrior)
/
Chap 09.01 - template
/
template.cp
< prev
next >
Wrap
Text File
|
1995-10-21
|
2KB
|
89 lines
#include <iostream.h>
const short kNumElements = 10;
//--------------------------------------- Array
template <class T>
class Array
{
private:
short arraySize;
T *arrayPtr;
T errorRetValue;
public:
Array( short size );
~Array();
T &operator[]( short index );
};
template <class T>
Array<T>::Array( short size )
{
arraySize = size;
arrayPtr = new T[ size ];
errorRetValue = 0;
}
template <class T>
Array<T>::~Array()
{
delete [] arrayPtr;
}
template <class T>
T &Array<T>::operator[]( short index )
{
if ( ( index < 0 ) || ( index >= arraySize ) )
{
cout << "index out of bounds(" << index << ")\n";
return( errorRetValue );
}
else
return( arrayPtr[ index ] );
}
//--------------------------------------- Power
template <class T>
T Power( T base, T exponent )
{
T i, product = 1;
for ( i=1; i<=exponent; i++ )
product *= base;
return( product );
}
//--------------------------------------- main()
int main()
{
Array<short> myRay( kNumElements );
Array<long> myLongRay( kNumElements );
short i, shortBase = 4;
long longBase = 4L;
for ( i=0; i<=kNumElements; i++ )
myRay[ i ] = Power( shortBase, i );
cout << "----\n";
for ( i=0; i<kNumElements; i++ )
cout << "myRay[" << i << "]: " << myRay[ i ] << "\n";
for ( i=0; i<kNumElements; i++ )
myLongRay[ i ] = Power( longBase, (long)i );
cout << "----\n";
for ( i=0; i<kNumElements; i++ )
cout << "myLongRay[" << i
<< "]: " << myLongRay[ i ] << "\n";
return 0;
}